﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Data.SqlClient;
using System.Data;
using SuraQR.Bean;
using System.Windows.Forms;

namespace SuraQR.Controller
{
    class OrcelConnection
    {

/*
        private MySqlConnection cnn;
        public void connect()
        {
            try
            {

                string connetionString = "Server=127.0.0.1;Uid=root;Pwd=root;Database=qr_db;";
                cnn = new MySqlConnection(connetionString);
                Debug.WriteLine(connetionString);
                cnn.Open();
                Debug.WriteLine("open");
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
            }
        }

        public void test()
        {
            try
            {
                string connetionString = "Server=127.0.0.1;Uid=root;Pwd=root;Database=qr_db;";
                var ccc = new MySqlConnection(connetionString);
                Debug.WriteLine(connetionString);
                ccc.Open();
                Debug.WriteLine("open");
                MessageBox.Show("OK {Server=127.0.0.1;Uid=root;Pwd=root;Database=qr_db}");
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }

        }

        public bool insert(SOROR12011 data)
        {
            try
            {
                MySqlCommand cmd = new MySqlCommand();
                cmd.CommandText = "BEGIN";
                cmd.Connection = cnn;
                cmd.CommandType = CommandType.Text;
                cmd.ExecuteNonQuery();

                // sr12
                string row_id = Guid.NewGuid().ToString();
                string sql = "insert into `sr120`(`row_Id`,`remark`)values( '?', '?' )";
                sql = this.replateValue(sql, new string[] {row_id,"OK" });
                cmd.CommandText = sql;
                cmd.ExecuteNonQuery();

                //F1Address
                sql = "insert into `address`(`name`,`factoryName`,`row_id`,`sr_id`)values('?','?', NULL,'?')";
                sql = this.replateValue(sql, new string[] { data.Addr.Name, data.Addr.FactoryName, row_id });
                cmd.CommandText = sql;
                cmd.ExecuteNonQuery();

                //order
                for (int i = 0; i < data.OrderList.Count(); i++ )
                {
                    Order o = data.OrderList[i];
                    sql = "insert into `orders`(`row_id`,`sr_id`,`alcohorName`,`alcohorType`)values(NULL,'?','?','?')";
                    sql = this.replateValue(sql, new string[] { row_id, o.AlcohorName, o.AlcohorType });
                    cmd.CommandText = sql;
                    cmd.ExecuteNonQuery();
                }

                //Tax
                sql = "insert into `tax`(`row_id`,`sr_id`,`taxReturn`,`taxPrepay`)values(NULL,'?','?','?')";
                sql = this.replateValue(sql, new string[] { row_id, data.Tax.TaxReturn.ToString(), data.Tax.TexPrepay.ToString() });
                cmd.CommandText = sql;
                cmd.ExecuteNonQuery();

                cmd.CommandText = "COMMIT";
                cmd.ExecuteNonQuery();
                return true;
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
                MySqlCommand cmd = new MySqlCommand();
                cmd.CommandText = "ROLLBACK";
                cmd.Connection = cnn;
                cmd.CommandType = CommandType.Text;
                cmd.ExecuteNonQuery();
                MessageBox.Show(e.ToString());
                return false;
            }
        }

        private string replateValue(string sql, string[] values)
        {
            int i = 0;
            while(sql.IndexOf("?") != -1){
                sql = this.ReplaceFirst(sql, "?", values[i]);
                i++;
            }
            Debug.WriteLine(sql);
            return sql;
        }

        private string ReplaceFirst(string text, string search, string replace)
        {
            int pos = text.IndexOf(search);
            if (pos < 0)
            {
                return text;
            }
            return text.Substring(0, pos) + replace + text.Substring(pos + search.Length);
        }

        public void close()
        {
            cnn.Close();
        }
 */
    }
}
